<?php


namespace app\admin\controller\sys;


use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\service\sys\AdminService;
use app\common\controller\BaseAdmin;
use app\admin\model\Admin as AdminModel;
use think\facade\Config;

class Admin extends BaseAdmin
{


    public function index(){
        if ($this->request->isAjax()){
            $data =$this->request->except(['page'],'get');

            //管理员id
            $auth = Config::get('login_admin.auth_uid');
            $admin_id = session($auth); //获取当前登录管理员id

            //end

            $model = new AdminModel();
            $list = $model->withSearch($this->searchFields($data),$this->searchData($data))
                ->order('id','desc')
                ->paginate($data['limit'], false);

            return $this->tableJson($list->getCollection(),$list->total());
        }

        //角色列表选择
        $roleList = AuthGroup::where('state',1)->order('id')->column('id,title');
        $this->assign('roleList',array_column($roleList,'title','id'));
        return $this->fetch();
    }

    /**
     * 新建/编辑模板
     * @return array|string
     */
    public function edit(){
        if ($this->request->isAjax()){

            //所有角色
            if ($this->request->isGet()){
                $roleList = AuthGroup::where('state',1)->field('id,title')->order('id')->select()->toArray();

                //将用户选中的角色
                $id = $this->request->get('id');
                $role = AuthGroupAccess::where('uid',$id)->column('group_id');
                return  json(['roleList'=>$roleList,'role'=>$role]);
            }


            $data = $this->request->param();
            //编辑或者添加数据
            $scene = empty($data['id'])?'add':'edit';
            return json(AdminService::storeAdmin($data,$scene));
        }

        $id = $this->request->get('id');
        if ($id){
            $list = AdminModel::where('id', $id)->find();
            $this->assign('list', $list);
        }

        return $this->fetch();
    }


    /**
     * 删除批量删除
     */
    public function remove(){
        if ($this->request->isAjax()){
            $ids = $this->request->param('ids');

            AdminModel::destroy($ids);

            //连带删除用户角色关系
            if (!is_array($ids)){
                $uid[] = $ids;
            }else{
                $uid = $ids;
            }

            AuthGroupAccess::where('uid','in',$uid)->delete();
            $this->success('删除成功');

        }else{
            $this->error('非法请求');
        }
    }

    /**
     * 验证管理员账号是否存在
     */
    public function check(){
        $username = $this->request->get('username', '', 'trim');
        $res = AdminModel::where('username', '=', $username)->field('id')->find();
        if ($res) {
            $this->success('账号已存在');
        } else {
            $this->error('验证通过');
        }
    }

    /**
     * 管理员审核
     */
    public function change(){
        $id = $this->request->param('id');
        $model = AdminModel::find($id);
        $model->state = $this->request->param('state');
        if ($model->save()){
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 我的资料
     */
    public function info(){
        if ($this->request->isAjax()){
            //所有角色
            if ($this->request->isGet()){
                $roleList = AuthGroup::where('state',1)->field('id,title')->order('id')->select()->toArray();

                //将用户选中的角色
                $role = AuthGroupAccess::where('uid',$this->auth_uid)->column('group_id');
                return  json(['roleList'=>$roleList,'role'=>$role]);
            }

            $data = $this->request->param();
            $data['id'] = $this->auth_uid;
            return json(AdminService::storeAdmin($data,'edit'));
        }
        $list = AdminModel::where('id', $this->auth_uid)->find();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 更改密码
     * @return string
     */
    public function password(){
        if ($this->request->isAjax()){
            $admin =  AdminModel::where('id', $this->auth_uid)->find();

            //旧密码检查
            $oldPassword = $this->request->param('oldPassword');
            if (md5($oldPassword.$admin->token)!=$admin->password){
                $this->error('当前密码错误');
            }

            $password =  $this->request->param('password');
            $admin->password = md5($password.$admin->token);
            if ($admin->save()){
                $this->success('新密码更改成功');
            }else{
                $this->error('新密码更改失败');
            }
        }
        return $this->fetch();
    }

}